package com.customview.CustomUsmileProgressBar;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import static android.graphics.Shader.TileMode.CLAMP;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/11/19 16:22
 */
public class UsmileProgressBar extends View implements View.OnClickListener {
    private String TAG = this.getClass().getName();

    public UsmileProgressBar(Context context) {
        this(context, null);
    }

    public UsmileProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UsmileProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    Paint mBarPaint;
    Paint mBarBgPaint;
    Paint mRriangPaint;//三角形
    Paint mTextPaint;//文字
    Path mRriangPath;
    int BasePadding = dipToPx(getContext(), 10);

    private void init() {
        mBarBgPaint = new Paint();
        mBarBgPaint.setStyle(Paint.Style.FILL);
        mBarBgPaint.setColor(Color.parseColor("#222329"));

        mBarPaint = new Paint();
        mBarPaint.setStyle(Paint.Style.FILL);
        mRriangPaint = new Paint();
        mRriangPaint.setStyle(Paint.Style.FILL);
        mRriangPaint.setColor(Color.BLUE);
        mTextPaint = new Paint();
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mRriangPath = new Path();
        setOnClickListener(this);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        setValues(100, true);

    }

    int DefWidth = dipToPx(getContext(), 240);
    int DefHeight = dipToPx(getContext(), 50);

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWith = MeasureSpec.getSize(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        //根据模式来 决定宽度
        switch (modeWidth) {

            case MeasureSpec.EXACTLY://match_parent  精确的度数
                DefWidth = sizeWith;
                break;
            case MeasureSpec.AT_MOST://wrap_content  适配的度数


                break;
        }


        //根据模式来 决定宽度
        switch (modeHeight) {

            case MeasureSpec.EXACTLY://match_parent  精确的度数
                DefHeight = sizeHeight;
                break;
            case MeasureSpec.AT_MOST://wrap_content  适配的度数


                break;
        }


        setMeasuredDimension(DefWidth, DefHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        drawLable(canvas);
        drawSorce(canvas);
        drawRriang(canvas);
        drawProgress(canvas);


    }

    String LableSorce = "80分";

    private void drawSorce(Canvas canvas) {

        float mtextHeight = (mTextPaint.getFontMetrics().bottom - mTextPaint.getFontMetrics().top) / 2;
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mTextPaint.setTextSize(24f);
        mTextPaint.setColor(Color.WHITE);
        canvas.drawText(LableSorce, BarLenght - BasePadding, getHeight() / 2 - BarHeigth / 2 - mtextHeight, mTextPaint);

    }

    String LableStart = "0分";
    String LableEnd = "100分";
    int textHeight = 24;


    private void drawLable(Canvas canvas) {


        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mTextPaint.setTextSize(24f);
        mTextPaint.setColor(Color.parseColor("#83838C"));
        canvas.drawText(LableStart, 0 + BasePadding, getHeight() / 2 + BarHeigth / 2 + textHeight, mTextPaint);
        mTextPaint.setTextAlign(Paint.Align.RIGHT);

        canvas.drawText(LableEnd, getWidth() - BasePadding, getHeight() / 2 + BarHeigth / 2 + textHeight, mTextPaint);

    }


    //    三角形
    int mRriangHeight = dipToPx(getContext(), 10);
    int mRriangWidth = dipToPx(getContext(), 14);

    //绘制三角形
    private void drawRriang(Canvas canvas) {


        mRriangPath.reset();

        mRriangPath.moveTo(BarLenght + BasePadding, getHeight() / 2 + BarHeigth / 2);
        mRriangPath.lineTo(BarLenght + mRriangWidth / 2 + BasePadding, getHeight() / 2 + BarHeigth / 2 + mRriangHeight);
        mRriangPath.lineTo(BarLenght - mRriangWidth / 2 + BasePadding, getHeight() / 2 + BarHeigth / 2 + mRriangHeight);
        mRriangPath.close();


        canvas.drawPath(mRriangPath, mRriangPaint);

    }


    int BarHeigth = dipToPx(getContext(), 10);


    //进度长度
    float BarLenght = 180;

    private void drawProgress(Canvas canvas) {


        RectF rectFBg = new RectF(0 + BasePadding, (getHeight() - BarHeigth) / 2, getWidth() - BasePadding, (getHeight() - BarHeigth) / 2 + BarHeigth);
        canvas.drawRoundRect(rectFBg, 20, 20, mBarBgPaint);

        mBarPaint.setShader(new LinearGradient(0f, 0f, getWidth(), 0f, new int[]{0xFF4E90FC, 0xFF2C7BFB, 0xFF3984FD}, new float[]{0f, 0.5f, 0.9f}, CLAMP));

        RectF rectF = new RectF(0 + BasePadding, (getHeight() - BarHeigth) / 2, BarLenght + BasePadding, (getHeight() - BarHeigth) / 2 + BarHeigth);

        canvas.drawRoundRect(rectF, 20, 20, mBarPaint);

    }


    /**
     * dip 转换成px
     *
     * @param dip
     * @return
     */
    public static int dipToPx(Context context, float dip) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }

    ValueAnimator animator;

    public void setValues(float values, boolean isAnimation) {

        Log.i(TAG, "setValues: width:" + getWidth());
        if (isAnimation) {
            if (animator != null) {
                animator.cancel();
                animator.setFloatValues(0, values);
                animator.start();
            } else {
                animator = ValueAnimator.ofFloat(0f, values);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {

                        float animatedValue = (float) valueAnimator.getAnimatedValue();
                        BarLenght = (float) (animatedValue * (getWidth() - BasePadding * 2) / 100);
                        Log.i(TAG, "setValues: valueAnimator:" + animatedValue);
                        Log.i(TAG, "setValues: BarLenght:" + BarLenght);

                        LableSorce = (int) animatedValue + "分";
                        postInvalidate();
                    }
                });
                animator.setDuration(3000);

            }
            animator.start();


        } else {
            BarLenght = (float) (values * (getWidth() - BasePadding * 2) / 100);
            LableSorce = (int) values + "分";

            postInvalidate();
        }
    }

    @Override
    public void onClick(View view) {
        setValues(100, true);

    }
}
